package com.jack.leetcode.每日一题;

import java.util.Arrays;

public class 最长连续序列 {

    public static void main(String[] args) {
        int[] nums = {100, 4, 200, 1, 3, 2};
        System.out.println(longestConsecutive(nums));
        int[] a = {1,2,0,1};
        System.out.println(longestConsecutive(a));
    }

    public static int longestConsecutive(int[] nums) {
        int result = 0;
        if (nums == null || nums.length == 0) {
            return result;
        }
        int n = nums.length;
        if(n == 1){
            return 1;
        }
        Arrays.sort(nums);
        int i = 0;
        while (i < n - 1) {
            int k = 1;
            while (i < n - 1) {
                if(nums[i + 1] - nums[i] == 1){
                    k++;
                    i++;
                }else if(nums[i + 1] - nums[i] == 0){
                    i++;
                }else{
                    break;
                }
            }
            i++;
            result = Math.max(k, result);
        }
        return result;
    }
}
